<template>
    <div>
      <BasicTable @register="registerTable">
        <template #toolbar>
          <a-button
            type="primary"
            @click="handleCreate"
          >
            新增
          </a-button>
          <a-button
            type="error"
            :disabled="getSelectRows().length === 0"
            @click="handleDelete"
            :loading="delLoading"
          > 
            批量删除
          </a-button>
        </template>
        <template #action="{ record }">
          <TableAction
            :actions="[
              {
                label: '编辑',
                onClick: handleEdit.bind(null, record),
              },
              {
                color: 'error',
                label: '删除',
                popConfirm: {
                  title: '是否确认删除',
                  confirm: handleDeleteSingle.bind(null, record),
                },
              },
            ]"
          />
        </template>
      </BasicTable>
      <EditModal @register="registerModal" @success="handleSuccess" />
    </div>
  </template>
  <script lang="ts">
  import { defineComponent } from 'vue';
import { defHttp } from '@/utils/http/axios';
  export default defineComponent({
    name: 'ChargeManager',
  });
  </script>
  <script lang="ts" setup>
  import { onMounted, ref } from 'vue';
  import { BasicTable, useTable, TableAction } from '@/components/Table';
  import { useModal } from '@/components/Modal';
  import EditModal from './EditModal.vue';
  import { useMessage } from '@/hooks/web/useMessage';
  const delLoading = ref(false);
  const { createMessage } = useMessage();
  const [registerModal, { openModal }] = useModal();
  const [registerTable, { reload, getSelectRows, getSelectRowKeys }] =
    useTable({
      api: '/admin/role/list',
      columns: [
        {
            title: 'ID',
            dataIndex: 'id',
            width: 50,
        },
        {
            title: '角色名',
            dataIndex: 'name',
            width: 90,
        }, 
        {
            title: '描述',
            dataIndex: 'description',
            width: 200,
        },
      ],
      useSearchForm: true,
      showIndexColumn: false,
      indexColumnProps: {
        fixed: 'left',
      },
      pagination: true,
      bordered: true,
      actionColumn: {
        width: 150,
        title: '操作',
        dataIndex: 'action',
        slots: { customRender: 'action' },
        fixed: 'right',
      },
      rowKey: 'id',
      rowSelection: {
        type: 'checkbox',
      },
    });
  onMounted(() => {
  });
  
  function handleCreate() {
    openModal(true, {
        isUpdate: false
    });
  }
  function handleEdit(record: Recordable) {
    openModal(true, {
        record,
        isUpdate: true,
    });
  }
  async function handleDelete() {
    delLoading.value = true;
    try {
        const res = await defHttp.post({ url: `/admin/role/delete`, params: getSelectRowKeys()});
        createMessage.success(res);
        reload();
    } finally {
        delLoading.value = false;
    }
  }
  async function handleDeleteSingle(record: Recordable) {
    const res = await defHttp.post({ url: `/admin/role/delete`, params: [record.id] });
    createMessage.success(res);
    reload();
  }
  function handleSuccess() {
    reload();
  }
  </script>
  